package com.rpay.admin.controller.question;

import com.rpay.admin.param.question.PostQuestion;
import com.rpay.admin.param.question.UpdateQuestion;
import com.rpay.admin.util.SessionUtil;
import com.rpay.common.dto.question.QuestionDTO;
import com.rpay.common.dto.question.QuestionQueryDTO;
import com.rpay.model.admin.RpayAdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.question.IQuestionService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/8
 * @Description:后台管理系统问题管理
 */
@RestController
@RequestMapping(value = "api/question")
@Validated
public class QuestionController {
    @Autowired
    private IQuestionService iQuestionService;

    @GetMapping(value = "")
    public PcsResult list(
            @RequestParam(name = "type", defaultValue = "") String questionType,
            @RequestParam(name = "keyword", defaultValue = "") String keyword,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QuestionQueryDTO query = new QuestionQueryDTO();
        query.setVisible(null);
        query.setQuestionType(questionType);
        query.setKeyword(keyword);
        query.setPage((page - 1) * pageSize);
        query.setPageSize(pageSize);
        QuestionQueryDTO data = iQuestionService.getDataList(query, page, pageSize);
        return PcsResult.build(data);
    }

    @PostMapping(value = "")
    public PcsResult add(HttpServletRequest request, @Validated @RequestBody PostQuestion postQuestion) {
        QuestionDTO question = new QuestionDTO();
        RpayAdminUser loginUser = SessionUtil.getCurrentUser();
        Integer userId = loginUser.getId().intValue();
        question.setUserId(userId);
        question.setQuestion(postQuestion.getQuestion());
        question.setQuestionType(postQuestion.getQuestionType());
        question.setEnQuestion(postQuestion.getEnQuestion());
        question.setAnswer(postQuestion.getAnswer());
        question.setEnAnswer(postQuestion.getEnAnswer());
        iQuestionService.saveQuestion(question);
        return new PcsResult();
    }

    @GetMapping(value = "/detail/{id}")
    public PcsResult detail(@PathVariable Integer id) {
        QuestionDTO query = new QuestionDTO();
        query.setId(id);
        query.setVisible(null);
        QuestionDTO questionDTO = iQuestionService.getDetail(query);
        return PcsResult.build(questionDTO);
    }

    @DeleteMapping(value = "/detail/{id}")
    public PcsResult delete(@PathVariable Integer id) {
        iQuestionService.deleteQuestion(id);
        return new PcsResult();
    }

    @PatchMapping(value = "/detail/{id}")
    public PcsResult update(@Validated @RequestBody UpdateQuestion updateQuestion, @PathVariable Integer id) {
        QuestionDTO questionDTO = new QuestionDTO();
        questionDTO.setId(id);
        questionDTO.setVisible(updateQuestion.getVisible());
        questionDTO.setQuestion(updateQuestion.getQuestion());
        questionDTO.setVisible(updateQuestion.getVisible());
        questionDTO.setQuestionType(updateQuestion.getQuestionType());
        questionDTO.setEnQuestion(updateQuestion.getEnQuestion());
        questionDTO.setAnswer(updateQuestion.getAnswer());
        questionDTO.setEnAnswer(updateQuestion.getEnAnswer());
        iQuestionService.updateQuestion(questionDTO);
        return new PcsResult();
    }
}
